<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\ProductArea;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class Up
{
    protected $order;
    protected $user;

    protected $set;

    protected $first_order_item;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();

        $this->first_order_item = $first_order_item;

        $product_id = $first_order_item->product_id;

        $this->set = SetUserUp::getLastOne();

        $is_bao = ProductArea::checkBao($product_id);
        $is_presale = ProductArea::checkPresale($product_id);

        // 必须是报单区
        if ($is_bao) {
            $this->upMyself();
            $this->upChain();
        }

    }

    // 升级自己
    protected function upMyself()
    {
        if ($this->first_order_item->price >= $this->set->vip1) {
            if ($this->user->level < UserLevel::$keys['vip1']) {
                $this->user->level = UserLevel::$keys['vip1'];
                $this->user->save();
            }
        }

        if ($this->order->box >= $this->set->vip2) {
            if ($this->user->level < UserLevel::$keys['vip2']) {
                $this->user->level = UserLevel::$keys['vip2'];
                $this->user->save();
            }
        }

    }

    // 升级关系链
    protected function upChain()
    {
        $chain = Chain::getReferrerChain($this->user->id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
            UserLevel::$keys['vip6'],
        ], true);

        // var_dump($this->user->id, $chain);exit;

        foreach ($chain as $user) {
            if ($user->level < UserLevel::$keys['vip6']) {
                $first = Chain::getChildrenLevel($user->id, [
                    UserLevel::$keys['vip1'],
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3'],
                    UserLevel::$keys['vip4'],
                    UserLevel::$keys['vip5'],
                    UserLevel::$keys['vip6'],
                ]);
                // var_dump(count($first));exit;
                if (count($first) >= 10) {
                    $box = $this->getBox($user->id);
                    if ($box >= $this->set->vip6_team) {
                        $level = $this->getLevel($user->id, UserLevel::$keys['vip5']);
                        // var_dump($level);exit;
                        if ($level) {
                            $user->level = UserLevel::$keys['vip6'];
                            $user->save();
                        }
                    }
                }
            }

            if ($user->level < UserLevel::$keys['vip5']) {
                $first = Chain::getChildrenLevel($user->id, [
                    UserLevel::$keys['vip1'],
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3'],
                    UserLevel::$keys['vip4'],
                    UserLevel::$keys['vip5'],
                    UserLevel::$keys['vip6'],
                ]);
                if (count($first) >= 10) {
                    $box = $this->getBox($user->id);
                    if ($box >= $this->set->vip5_team) {
                        $level = $this->getLevel($user->id, UserLevel::$keys['vip4']);
                        if ($level) {
                            $user->level = UserLevel::$keys['vip5'];
                            $user->save();
                        };
                    }
                }
            }

            if ($user->level < UserLevel::$keys['vip4']) {
                $first = Chain::getChildrenLevel($user->id, [
                    UserLevel::$keys['vip1'],
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3'],
                    UserLevel::$keys['vip4'],
                    UserLevel::$keys['vip5'],
                    UserLevel::$keys['vip6'],
                ]);
                if (count($first) >= 10) {
                    $box = $this->getBox($user->id);
                    if ($box >= $this->set->vip4_team) {
                        $level = $this->getLevel($user->id, UserLevel::$keys['vip3']);
                        // var_dump($level);exit;
                        if ($level) {
                            $user->level = UserLevel::$keys['vip4'];
                            $user->save();
                        }
                    }
                }
            }

            if ($user->level < UserLevel::$keys['vip3']) {
                $first = Chain::getChildrenLevel($user->id, [
                    UserLevel::$keys['vip1'],
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3'],
                    UserLevel::$keys['vip4'],
                    UserLevel::$keys['vip5'],
                    UserLevel::$keys['vip6'],
                ]);
                // var_dump(count($first));exit;
                if (count($first) >= 10) {
                    $box = $this->getBox($user->id);
                    if ($box >= $this->set->vip3_team) {
                        $level = $this->getLevel($user->id, UserLevel::$keys['vip2']);
                        // var_dump($level);exit;
                        if ($level) {
                            $user->level = UserLevel::$keys['vip3'];
                            $user->save();
                        }
                    }
                }
            }

            if ($user->level < UserLevel::$keys['vip2']) {
                $box = $this->getBox($user->id);
                if ($box >= $this->set->vip2_team) {
                    $user->level = UserLevel::$keys['vip2'];
                    $user->save();
                }
            }
        }
    }


    public function getBox($user_id)
    {
        $ids = Chain::getChildrenAll($user_id);
        $ids = ArrayHelper::getColumn($ids, 'id');

        // var_dump($ids);exit;

        $count = (int)Order::find()
            ->where(['user_id' => array_merge($ids, [$user_id])])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
            ->sum('box');

        return $count;

    }

    public function getLevel($user_id, $level)
    {
        $count = 0;

        $first = Chain::getChildrenLevel($user_id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
            UserLevel::$keys['vip6'],
        ]);

        foreach ($first as $user) {
            $x = Chain::getChildrenAllQuick($user['id'], [$level], ['id']);
            if ($x) {
                $count++;
            }
        }

        return $count >= 2;
    }

}